package com.tcu.order.controller;

import com.tcu.exception.SmartException;
import com.tcu.order.entity.OrderPicture;
import com.tcu.order.service.IOrderPictureService;
import com.tcu.result.R;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orderPicture")
public class OrderPictureController {
    private IOrderPictureService orderPictureService;

    @Autowired
    public void setOrderPictureService(IOrderPictureService orderPictureService) {
        this.orderPictureService = orderPictureService;
    }

    @PostMapping("add")
    public R add(@RequestBody OrderPicture orderPicture){
        if (orderPicture.getId()!=null) {
            throw new SmartException(20001,"id不为null，无法增加");
        }
        boolean b = orderPictureService.save(orderPicture);
        return b ? R.success() : R.error();
    }
    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable String id){
        boolean b = orderPictureService.removeById(id);
        return b ? R.success() : R.error();
    }

    @DeleteMapping("deleteByOrderId/{orderId}")
    public R deleteByOrderId(@PathVariable String orderId){
        Integer integer = orderPictureService.deletePicturesByOrderId(orderId);
        return integer!=0 ? R.success() : R.error();
    }

    @PostMapping("update")
    public R update(@RequestBody OrderPicture orderPicture){
        if (orderPicture.getId()==null) {
            throw new SmartException(20001,"id为null，无法更新！");
        }
        boolean b = orderPictureService.updateById(orderPicture);
        return b ? R.success() : R.error();
    }

    @GetMapping("getPicturesByOrderId/{orderId}")
    public R getPicturesByOrderId(@PathVariable String orderId){
        List<OrderPicture> picturesByOrderId = orderPictureService.getPicturesByOrderId(orderId);
        return R.success().data("pictures", picturesByOrderId);
    }
    @GetMapping("getSomePictures/{orderId}/{isUser}")
    public R getSomePictures(@PathVariable String orderId, @PathVariable Integer isUser){
        List<OrderPicture> somePictures = orderPictureService.getSomePictures(orderId, isUser);
        return R.success().data("pictures", somePictures);
    }


}
